<template>
  <el-card shadow="never" class="aui-card--fill managementWrapper">
    <div class="mod-sys__user">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="handleSearch()"
      >
        <el-form-item>
          <el-input
            v-model="dataForm.production_serial"
            placeholder="生产单号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataForm.name" placeholder="生产名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="dataForm.bills_date"
            type="date"
            placeholder="单据日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleSearch()">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toAdd()">创建计划</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        @sort-change="dataListSortChangeHandle"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="序号"
          header-align="center"
          align="center"
          width="80"
        ></el-table-column>
        <el-table-column
          prop="production_serial"
          label="生产单号"
          header-align="center"
          align="center"
          width="241"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="生产名称"
          width="180"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="bills_date"
          label="单据日期"
          sortable="custom"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="order_name"
          label="销售订单"
          width="180"
          header-align="center"
          align="center"
        >
        </el-table-column>
        <!-- <el-table-column
          prop="order_id"
          label="产品概要"
          width="180"
          header-align="center"
          align="center"
        >
        </el-table-column> -->
        <el-table-column
          prop="order_sub"
          label="产品概要"
          header-align="center"
          align="center"
          width="350"
        >
          <template slot-scope="scope">
            <div
              class="specBox disCenter"
              v-if="scope.row.order_sub.length > 0"
            >
              <el-tooltip
                class="item"
                effect="light"
                :content="scope.row.order_sub[0].product_name"
                placement="top"
              >
                <span class="overTwoHidden" style="max-width: 160px">{{
                  scope.row.order_sub[0].product_name
                }}</span>
              </el-tooltip>
              <el-popover placement="right" trigger="hover">
                <div
                  class="popover-box"
                  v-for="(item, index) in scope.row.order_sub"
                  :key="index"
                >
                  <div class="disCenter">
                    <div class="popover-img">
                      <img :src="item.product_master_pic" />
                    </div>
                    <div class="rightBox" style="margin-left: 12px">
                      <el-tooltip
                        class="item"
                        effect="light"
                        :content="item.product_name"
                        placement="top"
                      >
                        <span class="overTwoHidden" style="max-width: 150px">{{
                          item.product_name
                        }}</span>
                      </el-tooltip>
                      <div>
                        <span>规格：{{ item.sku_name }}</span>
                        <span style="margin-left: 12px"
                          >数量：{{ item.total }}</span
                        >
                      </div>
                    </div>
                  </div>
                </div>

                <!-- <div
                  class="mainColor"
                  style="text-align: center; margin-top: 12px; cursor: pointer"
                  @click="toDetail(scope.row)"
                  v-if="scope.row.order_sub.length > 4"
                >
                  查看详情
                </div> -->
                <span slot="reference" class="specDetail disCenter"
                  >等 {{ scope.row.order_sub.length }} 项
                  <i class="el-icon-caret-bottom"></i
                ></span>
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="customer_manager"
          label="负责人"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <!-- <el-table-column
          prop="order_price"
          label="状态"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column> -->
        <el-table-column
          prop="operator_user"
          label="最后操作人"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="operator_time"
          label="最后操作时间"
          sortable="custom"
          header-align="center"
          align="center"
          width="180"
        ></el-table-column>
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          width="210"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="toAdd(scope.row)"
              >编辑</el-button
            >
            <el-button type="text" size="small" @click="toDetail(scope.row)"
              >详情</el-button
            >
            <!-- <el-button
              type="text"
              size="small"
              @click="handleBomScan(scope.row)"
              >物料详情</el-button
            > -->
            <el-button
              type="text"
              size="small"
              @click="deleteHandle(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="num"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
      <el-dialog class="bom-dialog" :visible.sync="bomVisible" title="物料消耗">
        <MatterDetail :prodBomList="prodBomList"></MatterDetail>
        <template slot="footer">
          <el-button type="primary" @click="handleClose">关闭</el-button>
        </template>
      </el-dialog>
      <el-dialog :visible.sync="dialogVisible" title="查看">
        <img width="100%" :src="dialogImageUrl" class="avatar" />
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import { addDynamicRoute } from "@/router";
import { executeList } from "@/api";
import MatterDetail from "./matter-detail";
export default {
  mixins: [mixinViewModule],
  components: {
    MatterDetail,
  },
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/production",
        getDataListIsPage: true,
        deleteURL: "/production_delete",
        deleteIsBatch: true,
      },
      dataForm: {
        name: "",
        production_serial: "",
        bills_date: "",
      },
      value: "",
      bomVisible: false,
      prodBomList: [],
    };
  },
  created() {},
  methods: {
    handleClose() {
      this.bomVisible = false;
    },
    async handleBomScan(row) {
      const res = await executeList({ production_id: row.id });
      if (res?.status === 1) {
        this.prodBomList = res?.list || [];
        this.bomVisible = true;
      }
    },
    handleSearch() {
      this.dataForm.classify_ids = this.value;
      this.getDataList();
    },
    toDetail(row) {
      const routeParams = {
        routeName: `${this.$route.name}__${row.id}`,
        title: `${this.$route.meta.title} - 详情`,
        path: "manufacturing/plan-detail",
        params: {
          id: row.id,
        },
      };
      addDynamicRoute(routeParams, this.$router);
    },
    toAdd(row) {
      const des = row ? "编辑" : "新增";
      const type = row ? "edit" : "add";
      const routeParams = {
        routeName: `${this.$route.name}__${type}`,
        title: `${this.$route.meta.title} - ${des}`,
        path: "manufacturing/plan-add",
        params: {
          id: row?.id || "",
        },
      };
      addDynamicRoute(routeParams, this.$router);
    },
  },
};
</script>

<style lang="scss" scoped>
.managementWrapper {
  ::v-deep .el-dialog {
    width: 70% !important;
  }
  .specBox {
    .specDetail {
      margin-left: 12px;
      color: $mainColor;
      cursor: pointer;
    }
  }
  .prodBox {
    .imgBox {
      width: 64px;
      height: 64px;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .rightBox {
      margin-left: 14px;
      text-align: left;
      .top {
        max-width: 200px;
        margin-bottom: 6px;
      }
      .bottom {
        color: $mainColor;
      }
    }
  }
}
</style>

<!-- <style lang="scss" scoped>
      ::v-deep .el-table .cell {
        line-height: 19px !important;
      }
      </style> -->
